import 'package:flutter/services.dart';

final Action action = Action._();

enum LaunchResult {
  complete,
  notFound,
  error,
}

class Action {
  Action._();

  static const platform = MethodChannel('android/action');

  Future<void> mailto(String subject, String body) async {
    final data = 'mailto:example@example.com'
        '?subject=${Uri.encodeComponent(subject)}'
        '&body=${Uri.encodeComponent(body)}';
    await launch(data);
  }

  Future<LaunchResult> launch(String url) async {
    try {
      await platform.invokeMethod('launch', url);
      return LaunchResult.complete;
    } on PlatformException catch (e) {
      if (e.code == 'error_not_found') {
        return LaunchResult.notFound;
      } else {
        return LaunchResult.error;
      }
    } on Exception catch (_) {}
    return LaunchResult.error;
  }

  Future<bool> isADB() async {
    try {
      int adbEnabled = await platform.invokeMethod('settings', 'adb_enabled');
      return adbEnabled == 1;
    } catch (_) {}
    return false;
  }
}

///  final url = "intent://flutter.dev#Intent;package=com.android.chrome;action=android.intent.action.VIEW;category=android.intent.category.BROWSABLE;component=com.example.sample.MainActivity;scheme=https;end";
///  final url = "mailto:example@example.com?subject=${Uri.encodeComponent("Subject of the email")}&body=${Uri.encodeComponent("Body of the email")}";
///  final url = "tel:10086";
///  var canLaunch = await intent.launch(url);
///  if (canLaunch) {
///  }
